package nfwx.analytics;

import java.util.UUID;

/**
 * 采集会话基类
 */

public class GrabSession extends GrabObject {
    ///会话类型定义
    public static final String SESSION_TYPE_APP = "app";
    public static final String SESSION_TYPE_VIDEO = "video";

    //会话类型
    final String sessionType;
    //会话ID
    final String sessionId;
    //设备ID, 必选
    final public GrabString deviceId;
    //账户ID, 可选
    final public GrabString accountId;
    //使用时长, 可选
    final public GrabNumber useTimeDelta;
    //会话是否结束，可选
    final public GrabBoolean sessionEnd;
    //事件列表
    final public GrabArray events;
    //应用信息
    final public GrabAppInfo app;

    GrabSession(String sessionType)
    {
        super(true);
        this.sessionType = sessionType;
        this.sessionId = createSessionId();
        deviceId = (GrabString)defineProperty("deviceId", new GrabString(true));
        accountId = (GrabString)defineProperty("accountId", new GrabString(false));
        useTimeDelta = (GrabNumber)defineProperty("useTimeDelta", new GrabNumber(true));
        useTimeDelta.setValue(0);
        sessionEnd = (GrabBoolean)defineProperty("sessionEnd", new GrabBoolean(false));
        events = (GrabArray)defineProperty("events", new GrabArray(false));
        app = (GrabAppInfo)defineProperty("app", new GrabAppInfo());
    }

    /**
     * 获取会话是否已经结束
     * @return
     */
    public boolean getEnded()
    {
        return this.sessionEnd.getValue();
    }

    /**
     * 结束会话
     */
    void endSession()
    {
        this.sessionEnd.setValue(true);
    }

    ///创建会话ID
    static String createSessionId()
    {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }
}
